//
//  HESuggestViewController.m
//  HealthFamily
//
//  Created by LOOK on 2021/1/2.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HESuggestViewController.h"
#import "DLRadioButton.h"

@interface HESuggestViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;

// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) QMUITextField *titleTF;

@property (nonatomic, strong) QMUITextView *textView;


@property (nonatomic, strong) DLRadioButton *complaintBtn;

@property (nonatomic, strong) DLRadioButton *suggestBtn;
@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation HESuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"建议与反馈";
  
    [self initUI];
}

- (void)initUI {
    
    // 1. 添加scrollView
    [self.view addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentView];
 
    _titleTF = [[QMUITextField alloc] init];
    _titleTF.font = DYFont(16);
    _titleTF.placeholder = @"请输入标题";
    _titleTF.backgroundColor = KWhiteColor;
    _titleTF.textInsets = UIEdgeInsetsMake(5, 12, 5, 7);
    _titleTF.typingAttributes = @{NSFontAttributeName: UIFontMake(16),
                                  NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20],
                                  NSForegroundColorAttributeName: self.titleTF.textColor
    };
    [self.contentView addSubview: _titleTF];
    
    [_titleTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(0);
        make.left.equalTo(self.contentView).mas_offset(0);
        make.right.equalTo(self.contentView).mas_offset(-0);
        make.height.mas_greaterThanOrEqualTo(50);
    }];
    
    _textView = [QMUITextView new];
    // 限制可输入的字符长度
    self.textView.maximumTextLength = 1500;
    self.textView.placeholder = @"请输入内容";
    self.textView.placeholderColor = UIColorPlaceholder; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: UIFontMake(16),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.backgroundColor = KWhiteColor;
    [self.contentView addSubview:_textView];
    [_textView setDelegate:self];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleTF.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(0);
        make.right.equalTo(self.contentView).mas_offset(-0);
        make.height.mas_greaterThanOrEqualTo(180);
    }];
    
    
    _complaintBtn = [DLRadioButton new];
    _complaintBtn.tag = 1;
    _complaintBtn.marginWidth = 10;
    _complaintBtn.icon = [UIImage imageNamed:@"check_box_unsel"];
    _complaintBtn.iconSelected = [UIImage imageNamed:@"check_box_sel"];
    [_complaintBtn setTitle:@"投诉" forState:UIControlStateNormal];
    [_complaintBtn setTitleColor:CNavLightBlackColor forState:UIControlStateNormal];
    _complaintBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _complaintBtn.titleLabel.font = DYFont(15);
    [_complaintBtn addTarget:self action:@selector(radioSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_complaintBtn];

    
    _complaintBtn.selected = YES;
    _currentIndex = 1;
    
    _suggestBtn = [DLRadioButton new];
    _suggestBtn.tag = 2;
    _suggestBtn.marginWidth = 10;
    _suggestBtn.icon = [UIImage imageNamed:@"check_box_unsel"];
    _suggestBtn.iconSelected = [UIImage imageNamed:@"check_box_sel"];
    [_suggestBtn setTitle:@"建议" forState:UIControlStateNormal];
    [_suggestBtn setTitleColor:CNavLightBlackColor forState:UIControlStateNormal];
    _suggestBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _suggestBtn.titleLabel.font = DYFont(15);
    [_suggestBtn addTarget:self action:@selector(radioSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_suggestBtn];
    
    [_complaintBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textView.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).offset(15);
        make.width.mas_equalTo([@"投诉" widthForFont:DYFont(15)] + 40);
    }];
    
    [_suggestBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_complaintBtn);
        make.left.equalTo(_complaintBtn.mas_right).offset(20);
        make.width.mas_equalTo([@"建议" widthForFont:DYFont(15)] + 40);
    }];
    
    NSMutableArray *otherButtons = [NSMutableArray new];
    [otherButtons addObject:_suggestBtn];
    _complaintBtn.otherButtons = otherButtons;
    
  
    QMUIFillButton *agreeBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    agreeBtn.titleLabel.font = DYBlodFont(16);
    [agreeBtn setTitle:@"提 交" forState:UIControlStateNormal];
    [self.view addSubview:agreeBtn];
    [agreeBtn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];

    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_complaintBtn.mas_bottom).mas_offset(30);
        make.centerX.equalTo(self.contentView);
        make.left.equalTo(self.contentView).mas_offset(30);
        make.right.equalTo(self.contentView).mas_offset(-30);
        make.height.mas_offset(40);
    }];
}

- (void)radioSelectedButton:(DLRadioButton *)radioButton {
    NSInteger index = radioButton.tag;
    _currentIndex = index;
}

- (void)submit {
    
    if ([DYTools isBlankString:_titleTF.text]) {
        [Dialog toastBottom:@"请输入标题"];
        return;
    }
    
    if ([DYTools isBlankString:_textView.text]) {
        [Dialog toastBottom:@"请输入内容"];
        return;
    }
    
    NSMutableDictionary *req = @{}.mutableCopy;
    [req setValue:_titleTF.text forKey:@"title"];
    [req setValue:_textView.text forKey:@"content"];
    [req setValue:[NSString stringWithFormat:@"%ld", _currentIndex] forKey:@"type"];
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:Ksupport parameters:req success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            [Dialog toastBottom:@"操作成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(ZXNavBarHeight, 0, 0, 0));
    }];
    
    // 设置参照视图的约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
        make.height.offset(kScreenHeight);
    }];
}

#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = KWhiteColor;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = CViewBgColor;
    }
    return _contentView;
}

@end
